1874B - Jellyfish and Math - CodeForces Solution


bitmasks brute force dfs and similar dp graphs shortest paths

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,m;
inline int read()
{
    int s=0;
    char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s;
}
int he,ta,s[1010],t[1010000],e[1010100];
typedef pair<int,int> pa;
pa q[1010000];
map<pa,int>ma;
int main()
{
    int T=read();
    a = 99;//0 1 5 6
    b = 78;//1 2 3 6
    m = 120;//3 4 5 6
    he=0;ta=1;
    q[ta]=make_pair(a,b);t[1]=1;
    ma[make_pair(a,b)]=1;
    while(he<ta) {
        he++;
        int x=q[he].first,y=q[he].second;
        if(!ma[make_pair(x&y,y)]) ma[make_pair(x&y,y)]=t[ta+1]=t[he]+1,q[++ta]=make_pair(x&y,y);
        if(!ma[make_pair(x|y,y)]) ma[make_pair(x|y,y)]=t[ta+1]=t[he]+1,q[++ta]=make_pair(x|y,y);
        if(!ma[make_pair(x,y^x)]) ma[make_pair(x,y^x)]=t[ta+1]=t[he]+1,q[++ta]=make_pair(x,y^x);
        if(!ma[make_pair(x,y^m)]) ma[make_pair(x,y^m)]=t[ta+1]=t[he]+1,q[++ta]=make_pair(x,y^m);
    }
    // cout<<ta<<'\n';
    while(T--) {
        a=read(),b=read(),c=read(),d=read();m=read();
        ma.clear();
        s[6] = a & b & m;
        s[1] = (a & b) ^ s[6];
        s[3] = (b & m) ^ s[6];
        s[5] = (a & m) ^ s[6];
        s[0] = a ^ s[1] ^ s[5] ^ s[6];
        s[2] = b ^ s[1] ^ s[3] ^ s[6];
        s[4] = m ^ s[3] ^ s[5] ^ s[6];
        for(int i=0;i<128;i++) {
            int na=0;
            for(int j=0;j<7;j++) if(i&(1<<j)) na|=s[j];
            e[i]=na;
        }
        int flag=0;
        for(int i=1;i<=ta;i++) {
            int x = q[i].first,y=q[i].second;
            int na=e[x],nb=e[y];
            if(na==c&&nb==d) {flag=1;cout<<t[i]-1<<'\n';break;}
        }
        if(!flag) cout<<-1<<'\n';
        // cerr<<ta<<'\n';
    }
}


Comments

Submit
0 Comments
More Questions

1705A - Mark the Photographer
1707A - Doremy's IQ
1706B - Making Towers
1325B - CopyCopyCopyCopyCopy
1649C - Weird Sum
1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem